package org.hospital.entity;
// default package

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import static org.hibernate.criterion.Example.create;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 	* A data access object (DAO) providing persistence and search support for MedicalCard entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .MedicalCard
  * @author MyEclipse Persistence Tools 
 */
    @Transactional   
public class MedicalCardDAO  {
	     private static final Logger log = LoggerFactory.getLogger(MedicalCardDAO.class);
		//property constants
	public static final String CLIENT_ID = "clientId";
	public static final String BILL_ID = "billId";
	public static final String PROJECT_ID = "projectId";
	public static final String UNIT_PRICE = "unitPrice";
	public static final String CLINIC_COST = "clinicCost";
	public static final String EXCUTOR_COST = "excutorCost";
	public static final String TOTAL_NUMBER = "totalNumber";
	public static final String REST_NUMBER = "restNumber";
	public static final String STATUS = "status";

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory){
       this.sessionFactory = sessionFactory;
    }

    private Session getCurrentSession(){
     return sessionFactory.getCurrentSession(); 
    }
	protected void initDao() {
		//do nothing
	}
    
	public List getByProperties(Map properties){
		Criteria c = getCurrentSession().createCriteria(MedicalCard.class);
		c.add(Restrictions.allEq(properties));
		c.list();
		return c.list();
	}
	
	public List<MedicalCard> getByProjectAndClient(int projectId,int clientId) {
		 log.debug("getByProjectAndClient instance");
	        try {
	        	Criteria c = getCurrentSession().createCriteria(MedicalCard.class);
	        	c.add(Restrictions.eq("projectId", projectId));
	        	c.add(Restrictions.eq("clientId", clientId));
	        	c.add(Restrictions.eq("status",  0));
	        	c.addOrder(Order.asc("medicalCardId"));
	        	List<MedicalCard> result = c.list();
	        	log.debug("getByProjectAndClient successful");
	        	return result;
	        } catch (RuntimeException re) {
	            log.error("getByProjectAndClient failed", re);
	            throw re;
	        }
		
	}
	
    public void save(MedicalCard transientInstance) {
        log.debug("saving MedicalCard instance");
        try {
            getCurrentSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(MedicalCard persistentInstance) {
        log.debug("deleting MedicalCard instance");
        try {
            getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public MedicalCard findById( java.lang.Integer id) {
        log.debug("getting MedicalCard instance with id: " + id);
        try {
            MedicalCard instance = (MedicalCard) getCurrentSession()
                    .get("org.hospital.entity.MedicalCard", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List<MedicalCard> findByExample(MedicalCard instance) {
        log.debug("finding MedicalCard instance by example");
        try {
            List<MedicalCard> results = (List<MedicalCard>) getCurrentSession() .createCriteria("MedicalCard").add( create(instance) ).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding MedicalCard instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from MedicalCard as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getCurrentSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List<MedicalCard> findByClientId(Object clientId
	) {
		return findByProperty(CLIENT_ID, clientId
		);
	}
	
	public List<MedicalCard> findByBillId(Object billId
	) {
		return findByProperty(BILL_ID, billId
		);
	}
	
	public List<MedicalCard> findByProjectId(Object projectId
	) {
		return findByProperty(PROJECT_ID, projectId
		);
	}
	
	public List<MedicalCard> findByUnitPrice(Object unitPrice
	) {
		return findByProperty(UNIT_PRICE, unitPrice
		);
	}
	
	public List<MedicalCard> findByClinicCost(Object clinicCost
	) {
		return findByProperty(CLINIC_COST, clinicCost
		);
	}
	
	public List<MedicalCard> findByExcutorCost(Object excutorCost
	) {
		return findByProperty(EXCUTOR_COST, excutorCost
		);
	}
	
	public List<MedicalCard> findByTotalNumber(Object totalNumber
	) {
		return findByProperty(TOTAL_NUMBER, totalNumber
		);
	}
	
	public List<MedicalCard> findByRestNumber(Object restNumber
	) {
		return findByProperty(REST_NUMBER, restNumber
		);
	}
	
	public List<MedicalCard> findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll() {
		log.debug("finding all MedicalCard instances");
		try {
			String queryString = "from MedicalCard";
	         Query queryObject = getCurrentSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public MedicalCard merge(MedicalCard detachedInstance) {
        log.debug("merging MedicalCard instance");
        try {
            MedicalCard result = (MedicalCard) getCurrentSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(MedicalCard instance) {
        log.debug("attaching dirty MedicalCard instance");
        try {
            getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(MedicalCard instance) {
        log.debug("attaching clean MedicalCard instance");
        try {
                      	getCurrentSession().buildLockRequest(LockOptions.NONE).lock(instance);
          	            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static MedicalCardDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (MedicalCardDAO) ctx.getBean("MedicalCardDAO");
	}
}